package xyz.ssf.cloud.project.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import lombok.RequiredArgsConstructor;

import xyz.ssf.cloud.common.config.StorageConfig;
import xyz.ssf.cloud.common.core.domain.R;
import xyz.ssf.cloud.common.core.storage.FileService;
import xyz.ssf.cloud.common.core.storage.StorageFactory;
import xyz.ssf.cloud.common.core.storage.StorageType;

import java.util.HashMap;
import java.util.Map;

/**
 * 存储配置控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/storage")
public class StorageController {

    private final StorageConfig storageConfig;
    private final FileService fileService;
    private final StorageFactory storageFactory;

    /**
     * 获取存储配置
     */
    @GetMapping("/config")
    @PreAuthorize("@ss.hasPermission('system:storage:query')")
    public R<Object> getStorageConfig() {
        Map<String, Object> data = new HashMap<>();
        data.put("currentType", storageConfig.getType());
        data.put("availableTypes", fileService.getAvailableTypes());
        
        // 本地存储配置
        StorageConfig.Local local = storageConfig.getLocal();
        data.put("local", local);
        
        // 又拍云存储配置
        StorageConfig.UpYun upyun = storageConfig.getUpyun();
        // 不返回敏感信息
        upyun.setOperatorPwd(null);
        data.put("upyun", upyun);
        
        return R.success(data);
    }

    /**
     * 更新存储类型
     */
    @PutMapping("/type")
    @PreAuthorize("@ss.hasPermission('system:storage:edit')")
    public R<Object> updateStorageType(@RequestBody Map<String, String> params) {
        String type = params.get("type");
        
        if (type == null || type.isEmpty()) {
            return R.error("存储类型不能为空");
        }
        
        // 验证存储类型是否有效
        try {
            StorageType.getByCode(type);
        } catch (Exception e) {
            return R.error("无效的存储类型");
        }
        
        // 检查对应的存储策略是否可用
        if (storageFactory.getStorageStrategy(type) == null) {
            return R.error("该存储类型未实现或无法使用");
        }
        
        // 更新存储类型
        storageConfig.setType(type);
        
        return R.success("更新存储类型成功");
    }

    /**
     * 更新本地存储配置
     */
    @PutMapping("/local")
    @PreAuthorize("@ss.hasPermission('system:storage:edit')")
    public R<Object> updateLocalConfig(@RequestBody StorageConfig.Local config) {
        if (config == null) {
            return R.error("配置不能为空");
        }
        
        // 更新本地存储配置
        StorageConfig.Local local = storageConfig.getLocal();
        if (config.getStoragePath() != null && !config.getStoragePath().isEmpty()) {
            local.setStoragePath(config.getStoragePath());
        }
        
        if (config.getAccessPath() != null && !config.getAccessPath().isEmpty()) {
            local.setAccessPath(config.getAccessPath());
        }
        
        if (config.getDomain() != null) {
            local.setDomain(config.getDomain());
        }
        
        return R.success("更新本地存储配置成功");
    }

    /**
     * 更新又拍云存储配置
     */
    @PutMapping("/upyun")
    @PreAuthorize("@ss.hasPermission('system:storage:edit')")
    public R<Object> updateUpYunConfig(@RequestBody StorageConfig.UpYun config) {
        if (config == null) {
            return R.error("配置不能为空");
        }
        
        // 更新又拍云存储配置
        StorageConfig.UpYun upyun = storageConfig.getUpyun();
        
        if (config.getBucketName() != null && !config.getBucketName().isEmpty()) {
            upyun.setBucketName(config.getBucketName());
        }
        
        if (config.getOperatorName() != null && !config.getOperatorName().isEmpty()) {
            upyun.setOperatorName(config.getOperatorName());
        }
        
        if (config.getOperatorPwd() != null && !config.getOperatorPwd().isEmpty()) {
            upyun.setOperatorPwd(config.getOperatorPwd());
        }
        
        if (config.getDomain() != null && !config.getDomain().isEmpty()) {
            upyun.setDomain(config.getDomain());
        }
        
        return R.success("更新又拍云存储配置成功");
    }

    /**
     * 测试存储配置
     */
    @PostMapping("/test")
    @PreAuthorize("@ss.hasPermission('system:storage:edit')")
    public R<Object> testStorage(@RequestBody Map<String, String> params) {
        String type = params.get("type");
        
        if (type == null || type.isEmpty()) {
            return R.error("存储类型不能为空");
        }
        
        try {
            // 尝试获取指定类型的存储策略
            storageFactory.getStorageStrategy(type);
            return R.success("存储配置测试成功");
        } catch (Exception e) {
            return R.error("存储配置测试失败: " + e.getMessage());
        }
    }
} 